package com.wash.shoes.domain.order;

import com.alibaba.fastjson2.JSONObject;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.google.common.collect.Lists;
import com.lop.open.api.sdk.LopException;
import com.wash.shoes.common.enums.OrderStatusEnum;
import com.wash.shoes.domain.dto.MobileOrderDto;
import com.wash.shoes.domain.dto.MtUserAddress;
import com.wash.shoes.domain.jd.JdCreateOrder;
import com.wash.shoes.entity.OrderGoodPo;
import com.wash.shoes.entity.OrderPo;
import com.wash.shoes.entity.OrderTransportPo;
import com.wash.shoes.entity.ShopPo;
import com.wash.shoes.service.OrderGoodService;
import com.wash.shoes.service.OrderService;
import com.wash.shoes.service.OrderTransportService;
import com.wash.shoes.service.ShopService;
import com.wash.shoes.util.AddressCuttingUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * @author lijianguo
 * @Date 26/03/2025 21:44
 * 支付的回掉的处理
 **/
@Log4j2
@Component
public class PayNotifyOrderComponent {

    @Autowired
    OrderService orderService;

    @Autowired
    ShopService shopService;

    @Autowired
    OrderGoodService orderGoodService;

    @Autowired
    JdCreateOrder jdCreateOrder;

    @Autowired
    OrderTransportService orderTransportService;

    public String process(OrderPo orderPo) throws LopException {

        //判断用户是不是补差价订单
        boolean flag = orderPo.getOrderNo().startsWith("B");
        if (flag) {
            orderPo.setStatus(OrderStatusEnum.COMPLETED.getStatus());
            orderService.updateById(orderPo);
            return WxPayNotifyResponse.success("处理成功!");
        }
//        // 测试不走物流信息 D 是抖音的订单
//        flag = orderPo.getOrderNo().startsWith("D");
//        if (flag) {
//            orderPo.setStatus(OrderStatusEnum.PICK_UP_SHOES.getStatus());
//            orderService.updateById(orderPo);
//            return WxPayNotifyResponse.success("处理成功!");
//        }

        // 修改订单为取鞋中
        orderPo.setStatus(OrderStatusEnum.PICK_UP_SHOES.getStatus());
        orderService.updateById(orderPo);
        // 发送物流信息
        ShopPo shopPo = shopService.getById(orderPo.getShopId());
        MobileOrderDto mobileOrder = JSONObject.parseObject(orderPo.getTransJson(), MobileOrderDto.class);

        //二次分拣清洗地址处理
        //1、有衣鞋的单子寄一个地方，全是鞋子的寄一个地方
        //2、如果是寄到工厂的就获取清洗地址

        List<OrderGoodPo> orderGoods = orderGoodService.getOrderGoods(orderPo.getId());
        OrderGoodPo orderGoodPo = Optional.ofNullable(orderGoods)
                .orElse(Lists.newArrayList())
                .stream()
                .findFirst()
                .orElse(new OrderGoodPo());
        MtUserAddress shopAddress = AddressCuttingUtil.getDeliveryAddress(orderGoodPo, shopPo);

        /*MtUserAddress shopAddress = new MtUserAddress();
        shopAddress.setReceiverName(shopPo.getShopName());
        shopAddress.setReceiverMobile(shopPo.getPhone());
        StringBuilder fullAddress = new StringBuilder();
        fullAddress.append(shopPo.getProvincesName());
        fullAddress.append(shopPo.getCityName());
        fullAddress.append(shopPo.getCountryName());
        fullAddress.append(shopPo.getAddress());
        shopAddress.setReceiverAddress(fullAddress.toString());*/

        // 设置取鞋的地址
        OrderTransportPo orderTransportPo = jdCreateOrder.process(orderPo, mobileOrder.getReceiveUserAddress(), shopAddress, "取鞋", Long.parseLong(mobileOrder.getShoeCollectionDate()) / 1000);
        // 设置地址
        orderTransportPo.setDeliveryJson(com.alibaba.fastjson.JSONObject.toJSONString(mobileOrder.getSendBackUserAddress()));
        log.info("订单的送回地址为 [{}]  [{}]", orderTransportPo.getOrderNo(), orderTransportPo.getDeliveryJson());
        orderTransportService.saveOrUpdate(orderTransportPo);

        log.info("支付处理结果 {} ", JSONObject.toJSONString(mobileOrder));

        return WxPayNotifyResponse.success("处理成功!");
    }


}
